"""
读取并解析 gd32_data.xlsx文件
"""
import pandas as pd

# 0 1 2 3 4 5 6 7 8 9 10 11 12 13
# A B C D E F G H I J K  L  M  N

# 定义字母和索引对照表
LETTER_INDEX = {
    'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13   
}

file_name = 'CW32F003.xlsx'
def main():
    
    # 打开文件outout.csv
    f = open(file_name + '_output.csv', 'w')
    
    # 读取gd32.xlsx文件
    pd_data = pd.read_excel(file_name)
    # 提取Sheet1的前C-M列数据
    pd_data = pd_data.iloc[:, :10]
    # print(pd_data)
    for i in range(2, len(pd_data)):
        
        # 如果dat是None或nan或非1.0则跳过
        # if pin_id == None or "" == pin_id or pd.isna(pin_id) or pin_id!= 1.0:
        #     # print("无数据")
        #     continue
        
        # 取出A列内容，作为pins
        pin_id = pd_data.iloc[i, LETTER_INDEX["A"]]
        if type(pin_id) == str:
            pin_id = pin_id.replace(' ', '').replace('\n', '')
    
        # 取出D列数据, 逐个去除内容里的空格
        pin_name = pd_data.iloc[i, LETTER_INDEX["D"]]
        if type(pin_name) == str:
            pin_name = pin_name.replace(' ', '').replace('\n', '')
            
        # 取出H列内容，作为描述
        desc:str = pd_data.iloc[i, LETTER_INDEX["H"]]
        if type(desc) == str:
            desc = desc.replace(' ', '').replace('\n', '')
            
        # 取出I列，作为模拟功能
        analog_func = pd_data.iloc[i, LETTER_INDEX["I"]]
        if type(analog_func) == str:
            analog_func = analog_func.replace(' ', '').replace('\n', '')
            analog_func = analog_func.replace(",", "|")
            if analog_func == "nan": 
                analog_func = ""
        
        default_desc = desc
        default_desc = default_desc.replace(",", "|")
        # 将pins中连续的|替换成一个|
        default_desc = default_desc.replace("||", "|")
        default_desc = default_desc.removeprefix("|")
        default_desc = default_desc.removesuffix("|")
        default_desc = default_desc.strip()
        if default_desc == "":
            default_desc = "-"
            
        # print(pin_id, pin_name, default_desc)
        
    #     content = "".join(desc[1:]).replace(",", "|")
    #     if content == "":
    #         content = "-"
            
        content_lst = default_desc.split("|")
        
        timers = filter_content(content_lst, lambda item: "TIM" in item)
        usarts = filter_content(content_lst, lambda item: "USART" in item or "UART" in item)
        i2cs   = filter_content(content_lst, lambda item: "I2C" in item)
        spis   = filter_content(content_lst, lambda item: "SPI" in item)
        awts   = filter_content(content_lst, lambda item: "AWT" in item)
        outs   = filter_content(content_lst, lambda item: "OUT" in item)
        
        # 剩余的
        other_content = "|".join(content_lst)
        
        line = f"{pin_name},{pin_id},"
        line += f"{timers},{usarts},{i2cs},{spis},{awts},{outs},"
        line += f"{analog_func},{other_content}"
        print(line)
        
        f.write(line + "\n")
        
    print("len: ", len(pd_data))
    f.close()

def filter_content(content_lst: list, filter):
    peripherals = [item for item in content_lst if filter(item)]
    for item in peripherals: 
        content_lst.remove(item)
    peripherals = "|".join(peripherals)
    return peripherals



if __name__ == '__main__':
    main()
